package org.hbgl.geoentity.db.service;

import org.hbgl.geoentity.db.util.ApiUrlConfig;
import org.hbgl.geoentity.db.util.HttpUtil;
import org.hbgl.geoentity.db.util.MD5Util;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.*;

import static org.hbgl.geoentity.db.util.ApiUrlConfig.API_CODE_PUBLIC;


/**
 * 针对于http://api.idcode.org.cn接口的调用处理
 * @author zhanglei
 *
 */
@Service
public class IdcodeApiServiceImpl {

	private String API_KEY = API_CODE_PUBLIC;

	// private static Logger log=LoggerFactory.getLogger(IdcodeApiServiceImpl.class);

	private boolean containFile = false;

	private Map<String,File> files = new HashMap<String,File>();

	/**
	 * 对map集合参数进行排序
	 * @param params
	 * @return
	 */
	private String sortMapToString(Map<String,Object> params){
		System.out.println("4params=" + params!=null? params.toString(): null);
		List<String>   keylist=new  ArrayList<String>(params.keySet());
		Collections.sort(keylist,new Comparator<String>(){
             public int compare(String a,String b)   {
                 return   a.toString().toLowerCase().compareTo(b.toString().toLowerCase()); 
             } 
        });
		StringBuffer result=new StringBuffer();
        for(String s : keylist){
			System.out.println("s为：" + s);
			Object obj=params.get(s);
			if(obj instanceof File){
				containFile = true;
				files.put(s,(File) params.get(s));
				continue;
			}
			System.out.println("s值为：" + s + obj!=null?obj.toString(): null);
			if(obj!=null) {
				result.append(s+"="+obj.toString()+"&");
			}

		}
		return result.substring(0,result.length()-1);
	}

	/**
	 * get请求
	 * @param url
	 * @param params
	 * @return
	 */
	public String get(String url,Map<String,Object> params){
		HttpUtil http=new HttpUtil();
		String key=API_KEY;
		if(!params.isEmpty()){
			String urlParams=sortMapToString(params);
			if(url.indexOf("?")>-1){
				url=url+"&";
			}else{
				url=url+"?";
			}
			String hash= MD5Util.MD5Encode(url+urlParams+key).toUpperCase();
			urlParams+="&hash="+hash;
			String resultJson=http.get(ApiUrlConfig.BaseUrl_INTERNETTEST+url+urlParams);
			printResult("get json结果："+resultJson);
			return resultJson;
		}else{
			String hash=MD5Util.MD5Encode(url+key).toUpperCase();
			String resultJson=http.get(ApiUrlConfig.BaseUrl_INTERNETTEST+url+"&"+hash);
			printResult("get json结果："+resultJson);
			return resultJson;
		}
	}

	/***
	 * post请求
	 * @param url
	 * @param params
	 * @return
	 */
	public String post(String url,Map<String,Object> params){
		String urlParams=sortMapToString(params);
		String key=API_KEY;
		String encodeUrl=url;
		if(url.indexOf("?")>-1){
			encodeUrl=url+"&";
		}else{
			encodeUrl=url+"?";
		}
//		printResult("用于加密的字符串："+encodeUrl+urlParams+key);
		String hash=MD5Util.MD5Encode(encodeUrl+urlParams+key).toUpperCase();
		params.put("hash",hash);
		HttpUtil http=new HttpUtil();
		String resultJson = "";
		if(containFile){
			resultJson = http.postMapContainFile(ApiUrlConfig.BaseUrl_INTERNETTEST + url, params,files);
		}else {
			resultJson = http.postMap(ApiUrlConfig.BaseUrl_INTERNETTEST + url, params);
		}
		printResult("post json结果："+resultJson + "---" + url + "---" + ApiUrlConfig.BaseUrl_INTERNETTEST);
		return resultJson;
	}

	private void printResult(String rs){
		System.out.println(rs);
	}


}
